package com.zhongcheng.superplayer;

import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

import com.zhongcheng.superaudioplayer.Listener.WIOnErrorListener;
import com.zhongcheng.superaudioplayer.Listener.WIOnLoadListener;
import com.zhongcheng.superaudioplayer.Listener.WIOnTimeinfoListener;
import com.zhongcheng.superaudioplayer.Listener.WIOnpreparedListener;
import com.zhongcheng.superaudioplayer.WIPlayer.WIPlayer;
import com.zhongcheng.superaudioplayer.WlTimeInfoBean;
import com.zhongcheng.superaudioplayer.util.WlTimeUtil;

import java.io.File;


public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private Button mBtnStart, mBtnPause, mBtnResum, mBtnStop,mBtnLeftChannel,mBtnRightChannel
            ,mBtnDoubleChannel;
    private Button mBtnChPitch,mBtnChTem,mBtnChPitandTem,mBtnCom;
    private Button mBtnRecordStart,mBtnRecordPause,mBtnRecordResum;
    private SeekBar mSeekBar,mVolSeekBar;
    private WIPlayer mPlayer;
    private TextView mTvTime;
    private boolean isSeeking;
    private int mSeekedPositon;
    private File outFile;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mPlayer = new WIPlayer();

        outFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),"textplay.aac");
        initView();
        initEvent();
    }


    private void initView() {

        mBtnStart = findViewById(R.id.btn_start);
        mBtnPause = findViewById(R.id.btn_pause);
        mBtnResum = findViewById(R.id.btn_resum);
        mTvTime = findViewById(R.id.tv_time);
        mBtnStop = findViewById(R.id.btn_stop);
        mSeekBar = findViewById(R.id.sb_pregress_seek);
        mVolSeekBar = findViewById(R.id.sb_volume_seek);
        mBtnLeftChannel = findViewById(R.id.btn_left);
        mBtnRightChannel = findViewById(R.id.btn_right);
        mBtnDoubleChannel = findViewById(R.id.btn_double);

        mBtnChTem = findViewById(R.id.btn_ch_tem);
        mBtnChPitch = findViewById(R.id.btn_ch_pit);
        mBtnChPitandTem = findViewById(R.id.btn_ch_pit_tem);
        mBtnCom = findViewById(R.id.btn_common);

        mBtnRecordStart = findViewById(R.id.btn_record_start);
        mBtnRecordPause = findViewById(R.id.btn_record_pause);
        mBtnRecordResum = findViewById(R.id.btn_record_resum);
    }

    private void initEvent() {
        mBtnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPlayer.setSource("http://mpge.5nd.com/2015/2015-11-26/69708/1.mp3");
                mPlayer.prepare();
            }
        });

        mBtnPause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPlayer.pause();
            }
        });

        mBtnResum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPlayer.onResum();
            }
        });

        mPlayer.setPreparedListener(new WIOnpreparedListener() {
            @Override
            public void onPrePared() {
                Log.d(TAG, "准备好了，可以开始播放声音了");
                mPlayer.start();
            }
        });

        mPlayer.setmOnLoadListener(new WIOnLoadListener() {
            @Override
            public void onLoad(boolean load) {
                if (load) {
                    Log.d(TAG, "准备中");
                } else {
                    Log.d(TAG, "开始播放");
                }
            }
        });

        mPlayer.setmOnErrorListner(new WIOnErrorListener() {
            @Override
            public void onError(int code, String msg) {
                Log.e(TAG, code + msg);
            }
        });

        mPlayer.setmOnTimeinfoListener(new WIOnTimeinfoListener() {
            @Override
            public void onTimeinfo(WlTimeInfoBean wlTimeInfoBean) {
                Log.d(TAG, wlTimeInfoBean.toString());
                Message message = Message.obtain();
                message.what = 1;
                message.obj = wlTimeInfoBean;
                handler.sendMessage(message);
            }
        });

        mBtnStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPlayer.stop();
            }
        });

        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (isSeeking) {
                    mSeekedPositon = mPlayer.getDuration() * progress / 100;
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                isSeeking = true;
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                isSeeking = false;
                mPlayer.seek(mSeekedPositon);
            }
        });

        mVolSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                mPlayer.setVolume(progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        mBtnLeftChannel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPlayer.setMute(1);
            }
        });

        mBtnRightChannel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPlayer.setMute(0);
            }
        });

        mBtnDoubleChannel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPlayer.setMute(2);
            }
        });



        mBtnCom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPlayer.setPitch(1.0);
                mPlayer.setTempo(1.0);
            }
        });

        mBtnChTem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPlayer.setPitch(1.0);
                mPlayer.setTempo(1.5);
            }
        });

        mBtnChPitch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPlayer.setPitch(1.5);
                mPlayer.setTempo(1.0);
            }
        });


        mBtnChPitandTem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPlayer.setPitch(1.5);
                mPlayer.setTempo(1.5);
            }
        });

        mBtnRecordStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mPlayer.startRecord(outFile);
            }
        });

        mBtnRecordPause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPlayer.stopRecord();

            }
        });

        mBtnRecordResum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPlayer.resumeRcord();
            }
        });
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                WlTimeInfoBean wlTimeInfoBean = (WlTimeInfoBean) msg.obj;
                mTvTime.setText(WlTimeUtil.secdsToDateFormat(wlTimeInfoBean.getTotalTime(), wlTimeInfoBean.getTotalTime())
                        + "/" + WlTimeUtil.secdsToDateFormat(wlTimeInfoBean.getCurrentTime(), wlTimeInfoBean.getTotalTime()));
                if (!isSeeking){
                    mSeekBar.setProgress(wlTimeInfoBean.getCurrentTime()*100/wlTimeInfoBean.getTotalTime());
                }
            }
        }
    };
}
